/**
 * CopyRight © 2012 USTC SINOVATE SOFTWARE CO.LTD All Rights Reserved.
 */

package com.cmcc.zysoft.infocollection.mobile.service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.cmcc.zysoft.infocollection.mobile.dao.MMessageDao;
import com.cmcc.zysoft.infocollection.model.Message;
import com.cmcc.zysoft.infocollection.model.SystemUser;
import com.starit.common.dao.hibernate.HibernateBaseDao;
import com.starit.common.dao.service.BaseServiceImpl;

/**
 * @author 袁凤建
 * <br />邮箱: yuan.fengjian@ustcinfo.com
 * <br />描述: MMessageService.java
 * <br />版本: 1.0.0
 * <br />日期: 2013-8-23 上午11:14:07
 * <br />CopyRight © 2012 USTC SINOVATE SOFTWARE CO.LTD All Rights Reserved.
 */

@Service
public class MMessageService extends BaseServiceImpl<Message, String> {

	/**
	 * 日志.
	 */
	private static final Logger logger = LoggerFactory.getLogger(MMessageService.class);
	
	@Resource
	private MMessageDao mMessageDao;
	
	/**
	 * 属性名称: mSystemUserService.
	 * 类       型: MSystemUserService.
	 */
	@Resource
	private MSystemUserService mSystemUserService;
	
	@Override
	public HibernateBaseDao<Message, String> getHibernateBaseDao() {
		return this.mMessageDao;
	}
	
	/**
	 * 设置消息的已读状态.
	 * @param messageId 消息ID
	 * @param usercode 手机号
	 * @return map
	 */
	@Transactional
	public void readMessage(String messageId, String usercode) {
		List<SystemUser> list = this.mSystemUserService.findByNamedParam(new String[]{"userName", "delFlag"}, new Object[]{usercode, "0"});
		if(!list.isEmpty()) {
			try {
				Message message = this.mMessageDao.get(messageId);
				message.setMessageReadFlag("1"); //设置已读(0-未读,1-已读)
				message.setMobile(usercode);
				message.setReadDate(new Date());
				this.mMessageDao.update(message);
			} catch (Exception e) {
				e.printStackTrace();
				logger.debug("#########  阅读标识添加失败!  #########");
			}
		} else {
			logger.debug("#########  用户不存在或已删除!  #########");
		}
	}
	
	/**
	 * 通过消息ID获取消息详细信息.
	 * @param messageId 消息ID
	 * @return Map<String, Object>
	 */
	public Map<String, Object> getMessageById(String messageId) {
		return this.mMessageDao.getMessageById(messageId);
	}
}